#include "commonpage.h"
#include "ui_commonpage.h"
#include "listitembox.h"
#include <QDebug>
CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(ui->playAllBtn, &QPushButton::clicked, this, [=](){
       emit playAll(pageType);
    });

    connect(ui->pageMusicList, &QListWidget::clicked, this, [=](const QModelIndex &index){
       emit playMusicByIndex(this, index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setMusicListType(PageType musicListType)
{
    this->pageType = musicListType;
}

void CommonPage::setCommonPageUI(const QString &title, const QString &image)
{
    ui->pageTittle->setText(title);

    ui->musicImageLabel->setPixmap(QPixmap(image));

    ui->musicImageLabel->setScaledContents(true);

}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    // 将旧内容清空
    musicOfPage.clear();

    for(auto music : musicList)
    {
        switch (pageType)
        {
        case LIKE_PAGE:
            if(music.getIslike())
            {
                musicOfPage.push_back(music.getMusicId());
            }
            break;
        case LOCAL_PAGE:
            musicOfPage.push_back(music.getMusicId());
            break;
        case HISTORY_PAGE:
            if(music.getIsHistory())
            {
                musicOfPage.push_back(music.getMusicId());
            }
            break;
        default:
            qDebug() << "暂未支持";
        }
    }
}

// 负责将歌曲更新到界面
void CommonPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musicList);

    for(auto musicId : musicOfPage)
    {
        auto it = musicList.findMusicByMusicId(musicId);
        if(it == musicList.end())
            continue;

        // 测试
         ListItemBox* listItemBox = new ListItemBox(this);
         // 设置歌曲名称、作者、专辑名称更新到界面
         listItemBox->setMusicName(it->getMusicName());
         listItemBox->setMusicSigner(it->getMusicSinger());
         listItemBox->setMusicAlbum(it->getMusicAlbumn());
         listItemBox->setLikeMusic(it->getIslike());

         QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
         listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));

         ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);

         connect(listItemBox, &ListItemBox::setIsLike, this, [=](bool isLike){
             emit updataLikeMusic(isLike, it->getMusicId());
         });
    }

    // 触发窗口重绘   updata()将paintEvent事件放在事件循环队列中，没有立马处理
    repaint();
}

void CommonPage::addMusicToPlaylist(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto music : musicList)
    {
        switch (pageType)
        {
        case LIKE_PAGE:
            if(music.getIslike())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        case LOCAL_PAGE:
            playList->addMedia(music.getMusicUrl());
            break;
        case HISTORY_PAGE:
            if(music.getIsHistory())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        default:
            qDebug() << "未支持页面";
        }
    }
}

QString CommonPage::getMusicIdByIndex(int index)
{
    if(index >= musicOfPage.size())
    {
        qDebug() << "无此歌曲";
        return "";
    }

    return musicOfPage[index];
}
